package com.ruoyi.eqmonitor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.eqmonitor.smdtv.domain.SmdtvHydrologicStation;
import com.ruoyi.eqmonitor.smdtv.service.ISmdtvHydrologicStationService;
import com.ruoyi.eqmonitor.utils.HydrologicStationUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@SpringBootTest
@RunWith(SpringRunner.class)
public class HydrologicStation {

    @Resource
    private ISmdtvHydrologicStationService smdtvHydrologicStationService;

//    {
//        "success":true,
//            "schema":null,
//            "message":null,
//            "data": [
//        {
//            "id":"70801400            ",
//                "name":"坝头闸下                                              ",
//                "lng":121.306027,
//                "lat":28.596944,
//                "type":"DD",
//                "type_name":"堰闸水文站",
//                "address":"路桥区桐屿街道坝头村                                        ",
//                "areacode":"331004005276"
//        },
//        {
//            "id":"70801800            ",
//                "name":"路桥                                                ",
//                "lng":121.379944,
//                "lat":28.574638,
//                "type":"ZZ",
//                "type_name":"河道水位站",
//                "address":"路桥区路桥街道田洋王村磨石桥                                    ",
//                "areacode":"331004002221"
//        }
//        ],
//        "paging":null
//    }
    @Test
    public void save() {
        //清空再新增
        boolean remove = smdtvHydrologicStationService.remove(new LambdaQueryWrapper<>());
        if (remove){
            Map<String, String> param = new HashMap<>();
            String stations = HydrologicStationUtil.doGet("stations", param);
            JSONObject jsonObject = JSON.parseObject(stations);
            Object data = jsonObject.get("data");
            String dataString = JSONArray.toJSONString(data);
            List<String> dataList= JSONArray.parseArray(dataString,String.class);
            List<SmdtvHydrologicStation> list = new ArrayList<>();
            for (String s : dataList){
                JSONObject map = JSON.parseObject(s);
                String id = map.get("id").toString().trim();
                String name = map.get("name").toString().trim();
                String lng = map.get("lng").toString();
                String lat = map.get("lat").toString();
                String type = map.get("type").toString();
                String type_name = map.get("type_name").toString();
                String address = map.get("address").toString().trim();
                String areacode = map.get("areacode").toString();
                SmdtvHydrologicStation station = new SmdtvHydrologicStation();
                station.setId(id);
                station.setName(name);
                station.setLng(lng);
                station.setLat(lat);
                station.setType(type);
                station.setTypeName(type_name);
                station.setAddress(address);
                station.setAreacode(areacode);
                station.setUpdateTime(new Date());
                list.add(station);
            }
            boolean b = smdtvHydrologicStationService.saveBatch(list);
            System.out.println(b);
        }else {
            System.err.println(false);
        }
    }

}
